<?php

require_once dirname(__FILE__).'/BaseForm.class.php';

class MailForm extends BaseForm {
	public function __construct($mailConfig) {
		$this->mailConfig = $mailConfig;
		parent::__construct();
	}
	
	public function configure() {
		$this->addField(
			'name', // Атрибут "name" у элемента ввода 
			'Имя',
			new InputWidget(),
			new StringValidator(array(
				'min_length' => 2
			))
		);
		$this->addField(
			'surname', // Если атрибуты "name" совпадают, то на сервере можно будет получить доступ только к данным из последнего элемента
			'Фамилия',
			new InputWidget(),
			new StringValidator(array(
				'min_length' => 2
			))
		);
		$this->addField(
			'whats_up',
			'Как дела?',
			new TextareaWidget(),
			new StringValidator()
		);
	}

	public function getMessage() {
		$html = '<table>';
		foreach ($this->fields as $name=>$field) {
			$html .= '<tr><th>'.$field['label'].'</th><td>'.$field['value'].'</td></tr>';
		}
		$html .= '</table>';
		return $html;
	}
	
	public function mail() {
		$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
		$mail->IsSMTP(); // telling the class to use SMTP
		$mail->SMTPAuth   = true;                  // enable SMTP authentication
		$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
		$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
		$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
		$mail->CharSet = 'UTF-8';			// Set UTF-8
		$mail->Username   = "tatianamaluytinasownrobot@gmail.com";  // GMAIL username
		$mail->Password   = "RobotsRage";            // GMAIL password
		foreach ($this->mailConfig['addresses'] as $address=>$recipientName) {
			$mail->AddAddress($address, $recipientName);
		}
		$mail->SetFrom('TatianaMaluytinasOwnRobot@gmail.com', 'Tatiana Maluytina\'s Own Robot');
		$mail->Subject = 'Заполена новая анкета';
		$mail->MsgHTML($this->getMessage());
		$mail->Send();
	}
	
}